package miejuli;

import utils.HttpUtil;
import utils.JsonUtil;
import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/25.
 */
public class MeiJuLiPay {
    String tokenID="";
    String phone="";
    String goodId="";
    String dmId="";
    String businessId="";

    public MeiJuLiPay(String businessId,String goodId,String phone){
        this.businessId=businessId;
        this.goodId=goodId;
        this.phone=phone;
    }

    public void loginYangmao() throws IOException, URISyntaxException {
        String url="http://app.meilbh.com/app/user/login";
        Map<String,String> param=new HashMap<>();
        param.put("password","112768b271a4c9d8dc3228d155d5134382b40b1d6cb7045a7d986b1511724ad9");
        param.put("phone",phone);
        String rep=getResponseString(HttpUtil.getInstance().doGet(url,param));
        JSONObject json= JsonUtil.toJSONObject(rep);
        tokenID=json.getJSONObject("data").getString("tokenID");
        //System.out.println(rep+"==="+phone+"===登陆");
    }

    public int gotoauth() throws IOException, URISyntaxException {
        String url="http://app.meilbh.com/app/user/gotoauth";
        Map<String,String> param=new HashMap<>();
        String rep=getResponseString(HttpUtil.getInstance().doGet(url,tokenID,param));
        JSONObject json= JsonUtil.toJSONObject(rep);
        int status=json.getJSONObject("data").getJSONObject("authen").getInt("applyStatus");
        //System.out.println(rep+"==="+phone+"===检查是否成功");
//        if(status==2){
//            cardbinding();
//        }else{
//            System.out.println(phone);
//        }
        return status;
    }

    public void cardbinding() throws IOException, URISyntaxException {
        String url="http://app.meilbh.com/app/user/cardbinding";
        Map<String,String> param=new HashMap<>();
        param.put("bankArea","北京 北京");
        param.put("bankCardNum","6210958159155100052");
        param.put("bankName","邮储银行");
        param.put("city","北京");
        param.put("province","北京");
        param.put("realName","张利萍");
        getResponseString(HttpUtil.getInstance().doGet(url,tokenID,param));
        //System.out.println(getResponseString(HttpUtil.getInstance().doGet(url,tokenID,param))+"==="+phone+"===上传银行卡");
    }

    public void generateNewOrder() throws IOException, URISyntaxException {
        String url="http://app.meilbh.com/app/order/generateNewOrder";
        Map<String,String> param=new HashMap<>();
        param.put("businessId",businessId);
        param.put("consigneeId","");
        param.put("goodsIds",goodId);
        param.put("goodsNum","1");
        param.put("makeTime",System.currentTimeMillis()+"");
        param.put("note","");
        param.put("ordertype","1");
        String rep=getResponseString(HttpUtil.getInstance().doGet(url,tokenID,param));
        JSONObject json= JsonUtil.toJSONObject(rep);
        dmId=json.getJSONObject("data").getString("dmId");
        System.out.println(rep+"==="+phone+"===生成订单");
    }

    public void getOrderPay() throws IOException, URISyntaxException {
        String url="http://app.meilbh.com/app/order/getOrderPay";
        Map<String,String> param=new HashMap<>();
        param.put("discountType","4");
        param.put("dmId",dmId);
        param.put("pwd","8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92");
        param.put("typeC","1");
        System.out.println(getResponseString(HttpUtil.getInstance().doGet(url,tokenID,param))+"==="+phone+"===支付");
    }

    public void getOrderSubmit() throws IOException, URISyntaxException {
        String url="http://app.meilbh.com/app/order/getOrderSubmit";
        Map<String,String> param=new HashMap<>();
        param.put("dmId",dmId);
        param.put("status","0");
        String rep=getResponseString(HttpUtil.getInstance().doGet(url,tokenID,param));
        JSONObject json= JsonUtil.toJSONObject(rep);
        dmId=json.getJSONObject("data").getString("dmId");
        System.out.println(getResponseString(HttpUtil.getInstance().doGet(url,tokenID,param))+"==="+phone+"===确认收货");
    }


    public String getResponseString(HttpResponse response) throws IOException {
        return  EntityUtils.toString(response.getEntity());
    }
}
